package com.net.model.interceptor;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.drug.lib.base.BaseApplication;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 版权：星云飞救公司 版权所有
 *
 * @author zhouhequan
 * 版本：1.0
 * 创建日期：2021/10/25 or 9:46
 * 描述：版本过期处理拦截器
 */
public class VersionInterceptor implements Interceptor {
    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        Request.Builder versionHeader = chain.request().newBuilder();
        PackageManager pm;
        PackageInfo pi;
        pm = BaseApplication.mContext.getPackageManager();
        try {
            pi = pm.getPackageInfo(BaseApplication.mContext.getPackageName(), 0);
            versionHeader.header("Version", String.valueOf(pi.versionCode));
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return chain.proceed(versionHeader.build());
    }
}
